package com.huijie.core.configuration;

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 开启druid监控
 * @author Administrator
 *
 */
@Configuration
public class DruidConfiguration {
	/**
	 * 
	 * @return 注册一个servlet
	 */
	 @Bean
	 public ServletRegistrationBean druidStatViewServle(){
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
//		//设置白名单
//		servletRegistrationBean.addInitParameter("allow","127.0.0.1");
//		//设置黑名单
//		servletRegistrationBean.addInitParameter("deny","192.168.1.1");
//		servletRegistrationBean.addInitParameter("loginUsername","admin");
//	    servletRegistrationBean.addInitParameter("loginPassword","123456");
//	    //是否能够重置数据.
//	    servletRegistrationBean.addInitParameter("resetEnable","false");
		return servletRegistrationBean;
	 }
	 /**
	  * 
	  * @return 注册一个filter
	  */
	 @Bean
	 public FilterRegistrationBean druidStatFilter(){
		 FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter());
		 //添加过滤规则.
	     filterRegistrationBean.addUrlPatterns("/*");
	     //添加不需要忽略的格式信息.
	     filterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
	     filterRegistrationBean.addInitParameter("profileEnable", "true");
	     filterRegistrationBean.addInitParameter("principalCookieName", "USER_COOKIE");
	     filterRegistrationBean.addInitParameter("principalSessionName", "USER_SESSION");
	     return filterRegistrationBean;
	    }
}
